from thoughts.models import Thought 
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.models  import User


@csrf_exempt 
def search_a_thought(request):
    thoughts = []
    if 'q' in request.GET:
        q  = request.GET['q']
        thoughts =  Thought.objects.filter(title__contains = q)
    context = {
        'thoughts' : thoughts,
        'is_authenticated' : request.user.is_authenticated(),
        }
    return render_to_response(
        'search/search.html',
        context,
        context_instance = RequestContext(request),
        )                   
    
@csrf_exempt 
def search_users(request):
    users = []
    if 'q' in request.GET:
        q  = request.GET['q']
        users =  User.objects.filter(username__contains = q).exclude(pk=request.user.id)
    context = {
        'users' : users,
        'is_authenticated' : request.user.is_authenticated(),
        }
    return render_to_response(
        'search/search.html',
        context,
        context_instance = RequestContext(request),
        )                   


